import { goToDropPos } from "@/contrib/creep/harvester"
import { CreepRole, RoleDatas } from "@/role/types/role"
import CreepController from "../controller"


class RoleRemoteHarvester {

    static spawnRoomNames: string[] = ['E35S49', 'E39S52']
    static workRoomNames: string[][] = [['E34S48'], ['E38S52']]
    static sourceIndexs: number[][][] = [[[0, 1]], [[0, 1]]]

    // static roomNames: string[] = ['E35S48', 'E34S48']
    // static roomHarvesterIndexs: number[][] = [[0, 1], [2, 3]]
    /**
     *
     * @param creep
     * @returns
     */
    static loop(creep: Creep) {

        if (creep.ticksToLive < 5) {
            creep.drop(RESOURCE_ENERGY)
        }

        // 获取creep名字后缀数字编号
        const creepIndex = parseInt(creep.name.split('_')[1])

        const spawnRoomIndex = RoleRemoteHarvester.spawnRoomNames.indexOf(creep.memory.spawnRoom)
        const roomNames = RoleRemoteHarvester.workRoomNames[spawnRoomIndex]
        const roomHarvesterIndexs = RoleRemoteHarvester.sourceIndexs[spawnRoomIndex]

        // console.log('spawnRoomIndex spawnRoom', spawnRoomIndex, creep.memory.spawnRoom, roomNames, roomHarvesterIndexsss, RoleRemoteHarvester.roomHarvesterIndexs)

        let roomName: string
        for (const index in roomHarvesterIndexs) {
            const indexs = roomHarvesterIndexs[index]

            for (const i in indexs) {
                if (indexs[i] == creepIndex) {
                    roomName = roomNames[index]
                    break
                }
            }
        }
        // console.log('spawnRoomIndex creepIndex roomName', spawnRoomIndex, creepIndex, roomName)

        const memoryData: RoleDatas[CreepRole.RemoteHarvester] = creep.memory.data as RoleDatas[CreepRole.RemoteHarvester]

        if (!memoryData) {
            creep.log('memory error')
            return
        }

        if (!memoryData.harvestRoom || memoryData.harvestRoom != roomName) {
            memoryData.harvestRoom = roomName
        }

        const room = Game.rooms[memoryData.harvestRoom]
        if (!room || room.name != creep.room.name) {
            creep.goTo(new RoomPosition(25, 25, memoryData.harvestRoom))
            return
        }

        if (!memoryData.sourceId) {
            const remoteSource = CreepController.remoteHarvesterFindRemoteSource(creep)
        }

        if (Game.time % 2 == 0) {

            if (creep.store[RESOURCE_ENERGY] >= 10) {
                // harvester repair container
                const container = creep.pos.lookFor(LOOK_STRUCTURES).find(s => {
                    return s.structureType === STRUCTURE_CONTAINER
                }) as StructureContainer
                if (container && container.hits < container.hitsMax) {
                    memoryData.targetId = container.id
                }
            }
        }

        if (memoryData.targetId) {
            const target = Game.getObjectById(memoryData.targetId)
            if (creep.store[RESOURCE_ENERGY] == 0 || !target || target.hits >= target.hitsMax) {
                delete memoryData.targetId
            }
            const status = creep.repair(target)
            if (status == OK || status == ERR_NOT_ENOUGH_ENERGY)
            {
                delete memoryData.targetId
            }
        }

        if (memoryData.sourceId) {

            const remoteSource = Game.getObjectById(memoryData.sourceId)
            if (remoteSource) {
                const status = creep.harvest(remoteSource)
                goToDropPos(creep, remoteSource)
                if (status == OK) {
                    creep.memory.stand = true
                }
            }
        }
    }
}

export default RoleRemoteHarvester
